<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>万息金融-后台管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8"/>
    <link rel="stylesheet" href="/admin/css/font.css">
    <link rel="stylesheet" href="/admin/css/xadmin.css">
    <script src="/admin/js/jquery.min.js"></script>
    <script src="/admin/js/myAjax.js"></script>
    <script src="/admin/js/common.js"></script>
    <script src="/admin/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/admin/js/xadmin.js"></script>
    <script src="/admin/js/intercept.js"></script>


</head>

<body>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">

        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body ">
                    <form class="layui-form layui-col-space5">
                        <div class="layui-inline layui-show-xs-block">
                            <input type="text" name="name" placeholder="新闻检索" autocomplete="off"
                                   class="layui-input"></div>
                        <div class="layui-inline layui-show-xs-block">
                            <button class="layui-btn" lay-submit="" lay-filter="newsSearch">
                                <i class="layui-icon">&#xe615;</i></button>
                        </div>
                    </form>
                    <form class="layui-form layui-col-space5">
                        <div class="layui-inline layui-show-xs-block">
                            <input class="layui-input" autocomplete="off" placeholder="创建时间开始日" name="startTime"
                                   id="start">
                        </div>
                        <div class="layui-inline layui-show-xs-block">
                            <input class="layui-input" autocomplete="off" placeholder="创建时间截止日" name="endTime" id="end">
                        </div>
                        <!--  <div class="layui-inline layui-show-xs-block">
                              <input type="text" name="name" placeholder="请输入名字" autocomplete="off"
                                     class="layui-input"></div>-->

                        <div class="layui-inline layui-show-xs-block">
                            <select id="isCover" name="isCover">
                                <option disabled selected style='display: none;' value="">是否是封面</option>
                                <option value="0">否</option>
                                <option value="1">是</option>
                            </select>
                        </div>
                        <div class="layui-inline layui-show-xs-block">
                            <select id="isHot" name="isHot">
                                <option disabled selected style='display: none;' value="">是否是热点新闻</option>
                                <option value="0">否</option>
                                <option value="1">是</option>
                            </select>
                        </div>
                        <div class="layui-inline layui-show-xs-block">
                            <select id="newsCategory" name="newsCategory.id">
                                <option disabled selected style='display: none;'></option>
                            </select>
                        </div>
                        <div class="layui-inline layui-show-xs-block">
                            <input type="text" name="writer" placeholder="请输入作者" autocomplete="off"
                                   class="layui-input"></div>
                        <div class="layui-inline layui-show-xs-block">
                            <button class="layui-btn" lay-submit="" lay-filter="sreach">
                                <i class="layui-icon">&#xe615;</i></button>
                        </div>
                    </form>


                </div>
                <div class="layui-card-body ">
                    <table class="layui-table" id="test" lay-data="{toolbar: '#toolbarDemo',id:'test'}"
                           lay-filter="bcfilt">
                    </table>
                </div>
            </div>
        </div>
    </div>

</div>
</div>
</body>
<script>
    $(
        function setNewsCate() {
            let res = myAjax(ipAddress()+"/news/category/g", {}, "get");
            let html = "";
            for (let i = 0; i < res.data.length; i++) {
                html += '<option value="' + res.data[i].id + '">' + res.data[i].name + '</option>'
            }
            $('#newsCategory').append(html);
        })
</script>
<script type="text/html" id="imgHref">
    <img style="max-width: 50px" src="{{d.imgHref}}">
</script>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="update">编辑</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="delete">删除</a>
</script>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add"> 新增</button>
    </div>
</script>
<script>
    layui.use(['laydate', 'form', 'jquery', 'table', 'laypage'], function () {
        var laydate = layui.laydate
            , $ = layui.jquery
            , form = layui.form
            , layPage = layui.laypage
            , table = layui.table;


        //下拉框
        /*    var dropData = myAjax("/bCFindNoCondition", {}, "get");
            setDropMenu(dropData);
    
            function setDropMenu(data) {
                let html = "<select name='id' style='display: block'>";
                for (let i = 0; i < data.length; i++) {
                    html += '<option value="' + data[i].id + '">' + data[i].title + '</option>';
                }
                html += '</select>';
                $('#businessCate').html(html);
            }*/

        //执行一个laydate实例
        laydate.render({
            elem: '#start' //指定元素

        });

        //执行一个laydate实例
        laydate.render({
            elem: '#end' //指定元素
        });

        // 条件搜索
        form.on('submit(sreach)',
            function (data) {
                //执行渲染
                table.render({
                    elem: '#test'
                    , page: true
                    , url: ipAddress()+"/news/g"
                    , method: 'get'
                    , text: '没有数据'
                    , toolbar: '#toolbarDemo'
                    , where: data.field
                    , cols: [[
                        {field: 'writer', width: 120, sort: true, title: '作者'}
                        , {field: 'comeFrom', minWidth: 150, title: '来源'}
                        , {field: 'name', minWidth: 80, title: '名称'}
                        , {field: 'imgHref', minWidth: 80, event: 'showImg', templet: '#imgHref', title: '图片'}
                        , {field: 'content', minWidth: 80, event: 'showContent', title: '内容'}
                        , {
                            field: 'newsCategory', sort: true, minWidth: 80, title: '分类', templet: function (d) {
                                if (d.newsCategory != null) {
                                    return d.newsCategory.name;
                                }
                                else {
                                    return "未分配类别"
                                }
                            }
                        }
                        , {
                            field: 'isCover', sort: true, minWidth: 80, title: '封面', templet: function (d) {
                                return d.isCover == 1 ? '是' : '否';
                            }
                        }
                        , {
                            field: 'isHot', sort: true, minWidth: 80, title: '热点', templet: function (d) {
                                return d.isHot == 1 ? '是' : '否';
                            }
                        }
                        , {field: 'createTime', minWidth: 100, title: '创建时间'}
                        , {field: 'updateTime', sort: true, title: '修改时间'}
                        , {field: 'remark', title: '批注'}
                        , {fixed: 'right', width: 160, align: 'center', toolbar: '#barDemo', title: '操作'}
                    ]]
                });
                return false;
            })
        form.on('submit(newsSearch)',
            function (data) {
                //执行渲染
                table.render({
                    elem: '#test'
                    , page: true
                    , url: ipAddress()+"/search/newsSearch/g"
                    , method: 'get'
                    , text: '没有数据'
                    , toolbar: '#toolbarDemo'
                    , where: data.field
                    , cols: [[
                        {field: 'writer', width: 120, sort: true, title: '作者'}
                        , {field: 'comeFrom', minWidth: 150, title: '来源'}
                        , {field: 'name', minWidth: 80, title: '名称'}
                        , {field: 'imgHref', minWidth: 80, event: 'showImg', templet: '#imgHref', title: '图片'}
                        , {field: 'content', minWidth: 80, event: 'showContent', title: '内容'}
                        , {
                            field: 'newsCategory', sort: true, minWidth: 80, title: '分类', templet: function (d) {
                                if (d.newsCategory != null) {
                                    return d.newsCategory.name;
                                }
                                else {
                                    return "未分配类别"
                                }
                            }
                        }
                        , {
                            field: 'isCover', sort: true, minWidth: 80, title: '封面', templet: function (d) {
                                return d.isCover == 1 ? '是' : '否';
                            }
                        }
                        , {
                            field: 'isHot', sort: true, minWidth: 80, title: '热点', templet: function (d) {
                                return d.isHot == 1 ? '是' : '否';
                            }
                        }
                        , {field: 'createTime', minWidth: 100, title: '创建时间'}
                        , {field: 'updateTime', sort: true, title: '修改时间'}
                        , {field: 'remark', title: '批注'}
                        , {fixed: 'right', width: 160, align: 'center', toolbar: '#barDemo', title: '操作'}
                    ]]
                });
                return false;
            })
        // 方法渲染首次点击页面
        table.render({
            elem: '#test'
            , page: true
            , url: ipAddress()+"/news/g"
            , method: 'get'
            , text: '没有数据'
            , toolbar: '#toolbarDemo'
            , cols: [[
                {field: 'writer', width: 120, sort: true, title: '作者'}
                , {field: 'comeFrom', minWidth: 150, title: '来源'}
                , {field: 'name', minWidth: 80, title: '名称'}
                , {field: 'imgHref', minWidth: 80, event: 'showImg', templet: '#imgHref', title: '图片'}
                , {field: 'content', minWidth: 80, event: 'showContent', title: '内容'}
                , {
                    field: 'newsCategory', sort: true, minWidth: 80, title: '分类', templet: function (d) {
                        if (d.newsCategory != null) {
                            return d.newsCategory.name;
                        }
                        else {
                            return "未分配类别"
                        }
                    }
                }
                , {
                    field: 'isCover', sort: true, minWidth: 80, title: '封面', templet: function (d) {
                        console.log(d);
                        return d.isCover == 1 ? '是' : '否';
                    }
                }
                , {
                    field: 'isHot', sort: true, minWidth: 80, title: '热点', templet: function (d) {
                        return d.isHot == 1 ? '是' : '否';
                    }
                }
                , {field: 'createTime', minWidth: 100, title: '创建时间'}
                , {field: 'updateTime', sort: true, title: '修改时间'}
                , {field: 'remark', title: '批注'}
                , {fixed: 'right', width: 160, align: 'center', toolbar: '#barDemo', title: '操作'}
            ]]
        });
        //头部工具栏事件:  新增
        table.on('toolbar(bcfilt)', function (obj) {

            switch (obj.event) {
                case 'add':
                    layer.open({
                        title: '信息修改',
                        shade: 0.1,
                        area: ['1000px', '500px'],
                        type: 2,
                        content: './NewsAdd.html',
                    })
                    break;
            }
            ;
        });

        //单元格工具事件：编辑  删除
        table.on('tool(bcfilt)', function (obj) {
            var data = obj.data; //获得当前行数据 layui-laypage-count
            switch (obj.event) {
                case 'showImg':
                    layer.open({
                        type: 1,
                        title: false,
                        closeBtn: 0,
                        area: '516px',
                        skin: 'layui-layer-nobg', //没有背景色
                        shadeClose: true,
                        content: '<img style="display: inline-block; width: 100%; height: 100%;" src="' + data.imgHref + '">'
                    });
                    break;
                case 'showContent':
                    layer.open({
                        type: 1,
                        title: false,
                        closeBtn: 0,
                        area: ['516px', '400px'],
                        // skin: 'layui-layer-nobg', //没有背景色
                        shadeClose: true,
                        content: '<div>' + data.content + ' </div>'
                    });
                    break;

                case 'delete':
                    layer.confirm('确认要删除吗？', function (index) {
                        let indx = table.cache.test.length;
                        //发异步删除数据

                        let res = myAjax(ipAddress()+'/news/d/' + data.id, {}, "delete");
                        let pg = $(".layui-laypage-curr").children().eq(1).text();
                        if (res.count == 1) {
                            if (indx == 1) {
                                table.reload('test', {page: {curr: pg - 1}})
                            } else {
                                table.reload('test', {})
                            }
                            layer.msg('已删除!', {icon: 1, time: 1000});
                        } else {
                            layer.msg('删除失败!', {icon: 1, time: 1000});
                        }
                    });

                    break;
                case 'update':
                    let res = myAjax(ipAddress()+'/news/g/' + data.id, {}, "get");
                    sessionStorage.setItem("newsId", data.id);
                    /* let res2=myAjax("/newsCategory/g",{},"get");
                     let newsCate='';
                     newsCate+='<option value="' + res.data.newsCategory.id + '">' + res.data.newsCategory.name+ '</option>';
 
                     for(let i=0;i<res2.data.length;i++){
                         if(res2.data[i].id!=res.data.newsCategory.id) {
                             newsCate += '<option value="' + res2.data[i].id + '">' + res2.data[i].name + '</option>'
                         }
                     }*/

                    layer.open({
                        title: '信息修改',
                        shade: 0.1,
                        area: ['1000px', '500px'],
                        type: 2,
                        content: './NewsUpdate.html',
                        success: function (layero, index) {
                            var body = layer.getChildFrame('body', index);

                            body.find('form').find('#Logo').attr("src", res.data.imgHref);
                            body.find('form').find('#name').val(res.data.name);
                            body.find('form').find('#id').val(res.data.id);
                            // body.find('form').find('#layeditDemo').text(res.data.content);
                            body.find('form').find('#remark').val(res.data.remark);
                            body.find('form').find('#comeFrom').val(res.data.comeFrom);
                            body.find('form').find('#writer').val(res.data.writer);

                            let hotNum = myAjax(ipAddress()+"/news/getHot", {}, "get");
                            let coverNum = myAjax(ipAddress()+"/news/getCover", {}, "get");
                            if (hotNum == 3 && res.data.isHot == 0) {
                                body.find('form').find('#hot1').attr('disabled', 'true')
                            }
                            if (coverNum == 1 && res.data.isCover == 0) {
                                body.find('form').find('#cover1').attr('disabled', 'true')
                            }

                        }
                    })
                    break;
            }
            ;
        });
    })
    ;

</script>
</html>